package groups;

import java.io.Serializable;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import user.UserAdminManagedBean;
import java.util.ArrayList;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.persistence.CascadeType;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import lecture.Lecture;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import user.User;

/**
 * An exercise group consists of some lfeaders (at least one)
 * and some students
 * @author Thorsten Ludwig
 */

@Entity
public class ExerciseGroup implements Serializable {
    /**
     * the leaders of the exercise groups
     */
    @ElementCollection
    @LazyCollection(LazyCollectionOption.TRUE)
    private List<String> leaders;
    /**
     * the students of the exercise groups
     */
    @ElementCollection
    @LazyCollection(LazyCollectionOption.TRUE)
    private List<String> students;
    /**
     *
     */
    @OneToOne(cascade= CascadeType.ALL)
    private MeetingDate time;
    
    private String place;
    
    @ManyToOne
    private Lecture lecture;
    
    /**
     * 
     */
    private String name;
    @Id
    @GeneratedValue
    private Long id;

    public ExerciseGroup() {
    }

    /**
     * create an empty exercise group
     */
    public ExerciseGroup (String name, Lecture lecture, String place){
        this.students = new ArrayList<String>();
        this.leaders  = new ArrayList<String>();
        this.name = name;
        this.place=place;
        this.lecture = lecture;
    }
    
        /**
     * Adding a leader
     * @param leader user, who get a leader
     */
    public boolean addLeader(String leader) {
        System.out.println("Add Leader aufgerufen");
        FacesContext context = FacesContext.getCurrentInstance();                
        UserAdminManagedBean ab = context.getApplication().evaluateExpressionGet(context, "#{userAdminBean}", UserAdminManagedBean.class);
        User user = ab.getUser(leader);
        if (user == null) 
            return false;
        this.leaders.add(leader);
        return true;
    }
    
    /**
     * TODO: Just for testing
     */
    public boolean addLeader(String leader, UserAdminManagedBean ab) {
        User user = ab.getUser(leader);
        if (user == null) 
            return false;
        this.leaders.add(leader);
        return true;
    }

    /**
     * remove a leader from the exercise group
     * @param leader leader who will be removed from the exercise group
     */
    public void removeLeader(String leader) {
        this.leaders.remove(leader);
    }


    /**
     * add a student to the exercise group
     * @param student student who will be added
     */
    public void addStudent(String student) {
        this.students.add(student);
    }

    /**
     * remove a student from the exercise group
     * @param student student who will be removed from the exercise group
     */
    public void removeStudent(String student) {
        this.students.remove(student);
    }

    /**
     * This methods returns a unique name for the exercise Group.
     * It consists of the lecture name, the time and the room
     * @return unique name of the exercise group
     */
    public String getName() {
        return this.name;
    }

    public List<String> getLeaders() {
        return leaders;
    }

    public MeetingDate getTime() {
        return time;
    }

    public void setTime(MeetingDate time) {
        System.out.println("Zeit gesetzt");
        this.time = time;
    }

    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        System.out.println("Ort gesetzt");
        this.place = place;
    }

    public List<String> getStudents() {
        return students;
    }
    
    public void setName(String name) {
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Lecture getLecture() {
        return lecture;
    }

    public void setLeaders(List<String> leaders) {
        this.leaders = leaders;
    }

    public void setStudents(List<String> students) {
        this.students = students;
    }
}
